package icepuppet.test;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MyJFrame3 extends JFrame implements KeyListener {

    public MyJFrame3(){
        //设置长宽
        this.setSize(400, 400);
        //设置标题
        this.setTitle("事件练习");
        //设置界面始终位于最上方
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消默认居中
        this.setLayout(null);

        //给整个界面添加键盘监听
        this.addKeyListener(this);

        //显示
        this.setVisible(true);
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("按下不松");
    }

    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("松开");
        //获取键盘上按键的编号
        int keyCode = e.getKeyCode();
        System.out.println(keyCode);
    }
}
